/*
 * Copyright (c) 2011 Auzingers
 * Created by Oui-IT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.auzingers.docsorganizer.googleapi;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/** Utility class for accessing preference settings concerning Google Accounts. */
public final class GooglePreferencesUtil {

    private static final String PREF_ACCOUNT_NAME = "accountName";

    private static final String PREF_AUTH_TOKEN = "authToken";

    private static final String PREF_GSESSIONID = "gsessionid";

    private SharedPreferences settings = null;

    private static GooglePreferencesUtil instance = null;

    /**
     * Private singleton constructor, creates an internal settings instance.
     *
     * @param ctx Context, must be an application context
     */
    private GooglePreferencesUtil(Context ctx) {
        settings = PreferenceManager.getDefaultSharedPreferences(ctx);
    }

    /**
     * Returns a singleton instance of this class or creates a new one if it doesn't exist yet.
     *
     * @param ctx Context, must be an application context (only used if the singleton instance hasn't
     *        been created yet)
     * @return singleton instance of GooglePreferenceUtil
     */
    public static synchronized GooglePreferencesUtil getInstance(Context ctx) {
        if (instance == null) {
            instance = new GooglePreferencesUtil(ctx);
        }
        return instance;
    }

    public String getAuthToken() {
        return settings.getString(PREF_AUTH_TOKEN, null);
    }

    /**
     * Stores the authorization token to the preferences.
     * @param newToken token to store
     */
    public void setAuthToken(String newToken) {
        final SharedPreferences.Editor editor = settings.edit();
        editor.putString(PREF_AUTH_TOKEN, newToken);
        editor.commit();
    }

    /**
     * Returns the current gsessionid.
     * @return see above
     */
    public String getGSessionId() {
        return settings.getString(PREF_GSESSIONID, null);
    }

    /**
     * Sets the gsessionid.
     * @param newId id to set
     */
    public void setGSessionId(String newId) {
        final SharedPreferences.Editor editor = settings.edit();
        editor.putString(PREF_GSESSIONID, newId);
        editor.commit();
    }

    /**
     * Removes the saved authorization token id.
     */
    public void clearAuthToken() {
        final SharedPreferences.Editor editor = settings.edit();
        editor.remove(PREF_AUTH_TOKEN);
        editor.commit();
    }

    /**
     * Sets the account name AND removes the gsessionid.
     * @param name account name to set
     */
    public void setAccountNameAndRemoveSessionId(String name) {
        final SharedPreferences.Editor editor = settings.edit();
        editor.putString(PREF_ACCOUNT_NAME, name);
        editor.remove(PREF_GSESSIONID);
        editor.commit();
    }
}
